package com.paipaideli.ui.mine.order.fragment;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jakewharton.rxbinding2.view.RxView;
import com.paipaideli.R;
import com.paipaideli.common.base.BasePresenterFragment;
import com.paipaideli.common.http.Api;
import com.paipaideli.common.pullrefresh.PullRefreshView;
import com.paipaideli.common.utils.ImageUtil;
import com.paipaideli.common.utils.ToastUtil;
import com.paipaideli.ui.mine.order.OrderDetailActivity;
import com.paipaideli.ui.mine.order.adpater.OrderAdapter;
import com.paipaideli.ui.mine.order.bean.OrderBean;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import io.reactivex.android.schedulers.AndroidSchedulers;
import okhttp3.MediaType;
import okhttp3.RequestBody;

public class FailFragment extends BasePresenterFragment {

    Unbinder unbinder;
    @BindView(R.id.order_allrecycleview)
    PullRefreshView order_allrecycleview;

    private OrderAdapter orderAdapter;

    private int curPage = 1;
    private int total;
    private int pageSize;

    public static FailFragment newInstance() {
        FailFragment fragment = new FailFragment();
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_allorder, null);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    public void initView() {
        order_allrecycleview.setEnableRefresh(true);
        //设置是否在全部加载结束之后Footer跟随内容
        order_allrecycleview.setOnRefreshListener(refreshlayout -> {
            order_allrecycleview.showLoading();
            curPage = 1;
            orderAdapter.clear();

            initDate();
            order_allrecycleview.finishRefresh();
        });

        order_allrecycleview.setLoadmoreListener(refreshlayout -> {
            double p = Math.ceil(Double.valueOf(total)/Double.valueOf(pageSize));
            int page = Integer.valueOf((int) p);
            if (curPage < page) {
                curPage++;
                initDate();
            } else {
                //加载完成 并且 标记为没有更多数据
                refreshlayout.finishLoadmoreWithNoMoreData();
            }
            order_allrecycleview.finishLoadmore();
        });
        orderAdapter = new OrderAdapter();
        order_allrecycleview.setLayoutManager(new LinearLayoutManager(getActivity()));
        order_allrecycleview.setAdapter(orderAdapter);

    }

    @Override
    public void onStart() {
        super.onStart();
        order_allrecycleview.showLoading();
        orderAdapter.clear();
        initDate();
    }

    public void initDate() {
        JSONObject requestData = new JSONObject();
        try {
            requestData.put("curPage", curPage);
            requestData.put("pageSize", "10");
            requestData.put("status", "9");
        } catch (Exception e) {
            e.getMessage();
        }
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), requestData.toString());
        Api.getInstanceGson().orderList(body)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(request -> {
                    order_allrecycleview.finishLoading();
                    if (request.code.equals("200")) {
                        if (request.data.size() == 0){
                            order_allrecycleview.showEmpty(request.msg);
                            return;
                        }

                        total = request.total;
                        pageSize = request.pageSize;
                        orderAdapter.setDate(request.data);
                        orderAdapter.notifyDataSetChanged();
                    } else {
                        order_allrecycleview.showEmpty(request.msg);
                        ToastUtil.show(request.msg);
                    }
                }, throwable -> {
//                    order_allrecycleview.showError(throwable);
                    Log.e(TAG, "error", throwable);
                });
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
